package com.mty.mydemo.act.hello;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import com.litesuits.common.utils.BitmapUtil;
import com.litesuits.http.request.FileRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.litesuits.sdk.listener.AbsActivityHttpListener;
import com.mty.mydemo.R;
import com.mty.mydemo.act.BaseActivity;
import com.mty.mydemo.act.home.HomeActivity;
import com.qunduikj.sjb.sdk.AppSdk;
import com.qunduikj.sjb.sdk.config.ApiConfig;
import com.qunduikj.sjb.sdk.consts.AppConst;

import java.io.File;
import java.util.concurrent.TimeUnit;



public class WelcomeActivity extends BaseActivity {
    private static final String TAG = WelcomeActivity.class.getSimpleName();
    protected ImageView ivWelcome;

    protected File imgFile;
    private Bitmap bitmap = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_welcome);
        ivWelcome= (ImageView) findViewById(R.id.iv_welcome);
        try {
            imgFile = new File(getFilesDir(), AppConst.IMG_PATH_WELCOME);
            if (imgFile.exists()) {
                bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                ivWelcome.setImageBitmap(bitmap); //设置Bitmap
            }

            //ApplicationInfo info = getPackageManager()
            //        .getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
            //boolean isShouFa = info.metaData.getBoolean(AppConst.SHOUFA);
            //String quDao = info.metaData.getString("UMENG_CHANNEL");
            //logi("渠道： " + quDao + "  isShouFa: " + isShouFa);
            //if (isShouFa) {
            //    ll_shoufa.setVisibility(View.VISIBLE);
            //    switch (quDao) {
            //        case AppConst.QUDAO_360Assit:
            //            iv_shoufa.setImageResource(R.drawable.shoufa_360);
            //            break;
            //        default:
            //            iv_shoufa.setImageResource(R.drawable.ic_launcher);
            //            break;
            //    }
            //} else {
            //    ll_shoufa.setVisibility(View.GONE);
            //}

            // init images
            initLoadingImg();
            waitForAppInitial();
        } catch (Exception e) {
            e.printStackTrace();
            finish();
            return;
        }
    }

    public void waitForAppInitial() {
        // app：线程启动；act：启动线程等待：成功回调主线程。
        new Handler().postDelayed(uiThead, 2000);
    }

    Runnable uiThead = new Runnable() {
        @Override
        public void run() {
            if (!activity.isFinishing()) {
                if (AppSdk.getInstance().getDataKeeper().get(AppConst.KEY_IS_GUIDED, true)) {
                    if (true) {//是否登录 AppSdk.getInstance().getUser().isLogin()
                        startActivity(HomeActivity.class);//主界面
                    } else {//登录界面
                        //startActivity(LoginActivity.class);
                    }
                } else {//引导界面
                    //sdk.getMemoryData().isFirstInstalled = true;
                    //startActivity(GuideActivity.class);
                    //AppSdk.getInstance().getDataKeeper().put(AppConst.KEY_IS_GUIDED, true);
                }
                finish();
            }
        }
    };

    private void initLoadingImg() {
        // 1. 展示本地图片
        // 2. 网络加载存储-立即展示&存储下次展示

        String picUrl = ApiConfig.WELCOME_PIC;
        //        AppSdk sdk = AppSdk.getInstance();
        //        if (sdk.getDynamicData().getWelcomePic() < 0) {
        //            sdk.setDynamicData(sdk.getDynamicData().setWelcomePic(RandomUtil.getRandom(0, 6)));
        //        }
        //        switch (sdk.getDynamicData().getWelcomePic()) {
        //            case 0:
        //                picUrl = ApiConfig.WELCOME_PIC;
        //                break;
        //            case 1:
        //                picUrl = ApiConfig.WELCOME_PIC1;
        //                break;
        //            case 2:
        //                picUrl = ApiConfig.WELCOME_PIC2;
        //                break;
        //            case 3:
        //                picUrl = ApiConfig.WELCOME_PIC3;
        //                break;
        //            case 4:
        //                picUrl = ApiConfig.WELCOME_PIC4;
        //                break;
        //            case 5:
        //                picUrl = ApiConfig.WELCOME_PIC5;
        //                break;
        //        }
        AppSdk.getInstance().getLiteHttp().executeAsync(new FileRequest(picUrl, imgFile)
                .setMethod(HttpMethods.Get)
                .setCacheExpire(5, TimeUnit.MINUTES)
                .setHttpListener(new AbsActivityHttpListener<File>(activity) {
                    @Override
                    public void onSuccess(File file, Response<File> response) {
                        try {
                            if (!activity.isFinishing() && file.exists()) {
                                bitmap = BitmapUtil.getSmallBitmap(file.getPath(), 480, 800);
                                ivWelcome.setImageBitmap(bitmap); //设置Bitmap
                            }
                        } catch (Throwable e) {
                            e.printStackTrace();
                        }
                    }
                }));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
            System.gc();
        }
    }
}
